<template>
  <div>
    <!-- 
      area-list:地区列表
      show-postal	是否显示邮政编码
      show-delete	是否显示删除按钮
      show-set-default	是否显示默认地址栏
      show-search-result	是否显示搜索结果
      search-result	详细地址搜索结果
     -->
    <van-address-edit
      :area-list="areaList"
      :address-info="info"
      show-delete
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    >
      <div class="van-cell van-field">
        <div class="van-cell__title van-field__label"><span>标签</span></div>
        <div class="van-cell__value van-field__value">
          <div class="van-field__body">
            <van-radio-group v-model="label">
              <van-radio name="家" checked-color="#ff4400">家</van-radio>
              <van-radio name="公司" checked-color="#ff4400">公司</van-radio>
              <van-radio name="学校" checked-color="#ff4400">学校</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
    </van-address-edit>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import {
  reqaddresslist,
  reqaddressadd,
  reqaddressremove,
} from "../../http/http";
import { Toast } from "vant";
export default {
  data() {
    return {
      // 1.初始化数据
      areaList,
      label: "",
      info: {}, //初始值
    };
  },
  mounted() {
    let id = this.$route.query.id; //38或undefined
    if (id) {
      //编辑进来的
      reqaddresslist({
        uid: JSON.parse(localStorage.getItem("isLogin")).uid,
        id: id
      }).then((res) => {
        if (res.data.code == 200) {
          let info = res.data.list[0];
          this.info = {
            id: info.id,
            name: info.username,
            tel: info.userphone,
            addressDetail: info.useraddress,
            areaCode: info.regioncode,
            isDefault: info.status == 1,
          };
          this.label = info.label;
        }
      });
    }
  },
  methods: {
    onSave(e) {
      if (this.$route.query.id) {
        //修改
        reqaddressedit({
          id: e.id,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.label,
        }).then((res) => {
          if (res.data.code == 200) {
            Toast(res.data.msg);
            this.$router.replace("/a-list");
          }
        });
      } else {
        //2.添加逻辑
        reqaddressadd({
          uid: JSON.parse(localStorage.getItem("isLogin")).uid,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.label,
        }).then((res) => {
          if (res.data.code == 200) {
            console.log(res);
            Toast(res.data.msg);
            this.$router.replace("/a-list");
          }
        });
      }
    },
    onDelete() {
      // 删除
      reqaddressremove({
        id: this.$route.query.id,
      }).then((res) => {
        Toast(res.data.msg);
        this.$router.replace("/a-list");
      });
    },
  },
};
</script>

<style scoped lang="less">
@import "../../less/index.less";
.van-radio-group {
  display: flex;
  width: 100%;
}
.van-radio {
  margin: @margin20;
}
</style>